<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap 101 Template</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
{#    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">#}
{#    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>#}
{#    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>#}
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <script src="/static/css/bootstrap.min.js"></script>
    <script src="/static/css/jQuery.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h1 class="text-center">注册</h1>
            <form id="myform">
                {% csrf_token %}
                {% for form in form_obj %}
                    <div class="form-group">
                        <label for="">{{ form.label }}</label>
                         {{ form }}
                    <span style="color: red" class="pull-right">{{ form.error.0 }}</span>
                    </div>
                {% endfor %}
                <div class="form-group">
                    <label for="myfile">头像
                    {% load static %}
                        <img src="{% static 'img/111.png' %}" id="myimg" alt="" width="100" style="margin-left: 10px">
                    </label>
                    <input type="file" id="myfile" name="avatar" style=" display: none">
                </div>
                <input type="button" class="btn btn-primary pull-right" value="注册" id="id_commit">

            </form>

        </div>
    </div>
</div>
<script>
    $("#myfile").change(function (){
        let myFilereaderobj= new FileReader();
        let fileObj =$(this)[0].files[0];
        myFilereaderobj.readAsDataURL(fileObj)
        myFilereaderobj.onload=function(){
            $('#myimg').attr('src',myFilereaderobj.result)
        }
    })
    $('#id_commit').click(function (){
        let formdataobj =new FormData();
        $.each($('#myform').serializeArray(),function(index,obj){
            formdataobj.append(obj.name,obj.value)
        })
        formdataobj.append('avatar',$('#myfile')[0].files[0]);
        $.ajax({
            url:"/register/" ,
            type:"post"  ,
            data:formdataobj,
            contentType:false,
            processData:false,
            success:function (args){
                if (args.code==1000){
                    window.location.href=args.url
                }
                else{
                    $.each(args.msg,function(index, obj){
                        console.log(index,obj)
                        let targetid ='#id_'+index;
                        $(targetid).next().text(obj[0]).parent().addClass('has-error')
                    })
                }
            }
        })
    })
    $('input').focus(function(){
        $(this).next().text('').parent().removeClass('has-error')
    })
</script>
</body>
</html>